import Storage from 'storage';

const state = {
  clientWidth: 0,
  clientHeight: 0,
  size: '',
  authToken: Storage.getLocal('authToken'),
  refreshToken: Storage.getLocal('refreshToken'),
  user: Storage.getLocal('user')
};

const getters = {
  clientWidth: state => state.clientWidth,
  clientHeight: state => state.clientHeight,
  size: state => state.size,
  authToken: state => state.authToken,
  refreshToken: state => state.refreshToken,
  user: state => state.user
};

const actions = {
  resetClientWidth({
    commit
  }, value) {
    commit('setClientWidth', value);
    commit('resetSize', value);
  },

  resetClientHeight({
    commit
  }, value) {
    commit('setClientHeight', value);
    commit('resetSize', value);
  },

  setUser({
    commit
  }, user) {
    commit('setUser', user);
  },

  setAuthToken({
    commit
  }, authToken) {
    commit('setAuthToken', authToken);
  },

  setRefreshToken({
    commit
  }, refreshToken) {
    commit('setRefreshToken', refreshToken);
  }
};

const mutations = {
  setClientWidth(state, value) {
    state.clientWidth = value;
  },

  setClientHeight(state, value) {
    state.clientHeight = value;
  },

  resetSize(state, value) {
    state.size = `${state.clientWidth}*${state.clientHeight}`;
  },

  setUser(state, user) {
    Storage.setLocal('user', user);
    state.user = user;
  },

  setAuthToken(state, authToken) {
    Storage.setLocal('authToken', authToken);
    state.authToken = authToken;
  },

  setRefreshToken(state, refreshToken) {
    Storage.setLocal('refreshToken', refreshToken);
    state.refreshToken = refreshToken;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
